<?php

//分组

class Group extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('group_model');
	}

	public function index()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;
		
		$per_page = 20;

		$count = $this->group_model->get_count();
		$group_list = $this->group_model->get_page($p, $per_page, array(), '*', 'id asc');
		
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('group/index'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();
		
		$this->template->assign('list', $group_list);
		$this->template->assign('page', $this->page);
		$this->template->display('group/list');
	}

	//添加管理员分组页面
	protected function _insert_get()
	{
		$this->load->model('access_model');
		$access_list = $this->access_model->get_all();
		
		$this->template->assign('access_list', $access_list);
		$this->template->display('group/insert');
	}
	
	//提交添加管理员分组
	protected function _insert_post()
	{
		
		$name = trim(post('name')); //分组名称
		$remark = trim(post('remark')); //备注信息
		$access_list = post('access') ? implode(',', array_unique(array_map('intval', post('access')))) : ''; //分组权限
		
		if ($name == '')
		{
			msg('名称必须填写！', -1);
		}
		
		if ($this->group_model->exists(array('name' => $name)))
		{
			msg('分组名称已经存在！', -1);
		}
		
		$data = array(
			'name' => $name,
			'remark' => $remark,
			'access_list' => $access_list
		);
		
		if ( ! ($id = $this->group_model->insert($data)))
		{
			log_message('error', '添加管理员分组失败！原因：写入group表失败！');
			msg('添加分组失败！');
		}
		
		$msg = '添加成功！<br /><br /><a href="'.admin_url('group/update', array('id' => $id)).'" class="button">返回修改</a>&nbsp;&nbsp;&nbsp;<a href="'.admin_url('group/insert').'" class="button">继续添加</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//修改页面
	protected function _update_get()
	{
		$id = intval(get('id'));
		if ($id < 1)
		{
			msg('非法访问！');
		}
		
		$group_data = $this->group_model->get($id);
		if ( ! $group_data)
		{
			msg('分组不存在！', admin_url('group/index'));
		}
		$group_data['access_list'] = explode(',', $group_data['access_list']);
		
		$this->load->model('access_model');
		$access_list = $this->access_model->get_all();
		
		$this->template->assign('data', $group_data);
		$this->template->assign('access_list', $access_list);
		$this->template->display('group/update');
	}
	
	//提交修改信息
	protected function _update_post()
	{		
		$id = intval(post('id')); //分组id
		$name = trim(post('name')); //分组名称
		$remark = trim(post('remark')); //备注信息
		$access_list = post('access') ? implode(',', array_unique(array_map('intval', post('access')))) : ''; //分组权限
		
		if ($id < 1)
		{
			msg('非法访问！');
		}
		
		$group_data = $this->group_model->get($id);
		if ( ! $group_data)
		{
			msg('分组不存在！', admin_url('group/index'));
		}
		
		if ($name != $group_data['name'] && $this->group_model->exists(array('name' => $name)))
		{
			msg('分组名称已经存在！', -1);
		}
		
		$data = array(
			'name' => $name,
			'remark' => $remark,
			'access_list' => $access_list
		);
		
		if (FALSE === $this->group_model->update($data, $id))
		{
			log_message('error', '修改管理员分组失败！原因：更新group表失败！');
			msg('修改失败！');
		}
		
		$msg = '修改成功！<br /><br /><a href="'.admin_url('group/update', array('id' => $id)).'" class="button">继续修改</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//删除管理员分组
	public function _delete_ajax()
	{
		$id = intval(get('id'));
		
		if ($id < 1)
		{
			ajax(array('code' => 1, 'msg' => '非法访问！'));
		}

		if ($id == 1)
		{
			ajax(array('code' => 2, 'msg' => '此分组不能删除！'));
		}
		
		$this->load->model('admin_model');
		if ($this->admin_model->exists(array('group_id' => $id)))
		{
			ajax(array('code' => 3, 'msg' => '必须首先删除分组下的用户！'));
		}
		
		$result = $this->group_model->delete($id);
		
		if ($result === FALSE)
		{
			log_message('error', '删除管理员分组失败！原因：更新group表失败！');
			ajax(array('code' => 4, 'msg' => '删除失败！'));
		}
		else
		{
			ajax(array('code' => 0, 'msg' => '删除成功！'));
		}
	}
	
}